#include <iostream>

using namespace std;
#define int long long
int x , y;
int ret;
signed main()
{
    cin >> x >> y;
    while(x && y)
    {
        int t = x / y;
        ret += t * y * 4;
        x %= y;
        swap(x , y);
    }
    cout << ret << endl;
    return 0;
}